import 'package:flutter/material.dart';
import 'package:flutter_demo/constants/constants.dart';
import 'package:flutter_demo/ui/graphQL_test/api/login_request.dart';
import 'package:flutter_demo/utils/context/one_context_utils.dart';

import '../../generated/l10n.dart';

///@date:  2024/1/18
///@author:  ZhaoPing
///@description: graphql 登录测试类

class HqlLoginView extends StatefulWidget {
  const HqlLoginView({super.key});

  @override
  State<StatefulWidget> createState() => _CountryPageState();
}

class _CountryPageState extends State<HqlLoginView> {
  final TextEditingController _unameController = TextEditingController();
  final TextEditingController _pwdController = TextEditingController();
  final GlobalKey _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Login'),
      ),
      body: _buildList(),
    );
  }

  _query() async {
    var api = LoginGraphQLRequest();
    if (Constants.token == null) {
      var result = await api.login(_unameController.text, _pwdController.text);
      print(result);
    } else {
      var result = await api.refreshToken();
      print(result);
    }
  }

  @override
  void initState() {
    _unameController.text = "18382062326";
    // TODO: implement initState
    super.initState();
  }

  Widget _buildList() {
    return Form(
      key: _formKey, //设置globalKey，用于后面获取FormState
      autovalidateMode: AutovalidateMode.onUserInteraction,
      child: Column(
        children: <Widget>[
          TextFormField(
            autofocus: true,
            // initialValue: "18382062326",
            controller: _unameController,
            decoration: InputDecoration(
              labelText: S.current.userName,
              hintText: S.current.userInputHint,
              icon: const Icon(Icons.person),
            ),
            // 校验用户名
            validator: (v) {
              return v!.trim().isNotEmpty ? null : S.current.userCheckTips;
            },
          ),
          TextFormField(
            controller: _pwdController,
            decoration: InputDecoration(
              labelText: S.current.password,
              hintText: S.current.passwordInputHint,
              icon: const Icon(Icons.lock),
            ),
            obscureText: true,
            //校验密码
            validator: (v) {
              return v!.trim().length > 5 ? null : S.current.passwordCheckTips;
            },
          ),
          // 登录按钮
          Padding(
            padding: const EdgeInsets.only(top: 28.0),
            child: Row(
              children: <Widget>[
                Expanded(
                  child: ElevatedButton(
                    child: Padding(
                      padding: const EdgeInsets.all(16.0),
                      child: Text(S.current.login),
                    ),
                    onPressed: () {
                      // 通过_formKey.currentState 获取FormState后，
                      // 调用validate()方法校验用户名密码是否合法，校验
                      // 通过后再提交数据。
                      if ((_formKey.currentState as FormState).validate()) {
                        //验证通过提交数据
                        _query();
                      }
                    },
                  ),
                ),
              ],
            ),
          ),
          // 修改密码按钮
          Padding(
            padding: const EdgeInsets.only(top: 28.0),
            child: Row(
              children: <Widget>[
                Expanded(
                  child: ElevatedButton(
                    child: Padding(
                      padding: const EdgeInsets.all(16.0),
                      child: Text(S.current.changePassword)
                    ),
                    onPressed: () {
                      // 通过_formKey.currentState 获取FormState后，
                      // 调用validate()方法校验用户名密码是否合法，校验
                      // 通过后再提交数据。
                      if ((_formKey.currentState as FormState).validate()) {
                        //验证通过提交数据
                        _changePwd();
                      }
                    },
                  ),
                ),
              ],
            ),
          )
        ],
      ),
    );
  }

  _changePwd() async {
    var api = LoginGraphQLRequest();
      var result =
          await api.changePassword(_unameController.text, _pwdController.text);
      print(result);

  }
}

class HqlLoginApp extends StatelessWidget {
  const HqlLoginApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '零售宝',
      theme: ThemeData(
          colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
          useMaterial3: true),
      home: HqlLoginView(),
      builder: OneContextUtils.oneContextBuilder,
      navigatorKey: OneContextUtils.oneContextKey,
    );
  }
}
